# 外部抓取信息的相关操作

import requests
import pymssql
from lxml import etree


def external_scraping(r, i):
    img = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/a/img/@src")
    title_text = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[1]/a/text()")
    title_href = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[1]/a/@href")
    diZhi_left = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[2]/div/a[1]/text()")
    diZhi_rigth = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[2]/div/a[2]/text()")
    house_type = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[3]/div/text()")
    kuaiSu = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[5]/span/text()")
    jiaGe = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[6]/div[1]/span/text()")
    jiaGe_danWei = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[6]/div[1]/i[2]/text()")
    jiaGe_m = r.xpath(f"//*[@id='content']/div[1]/ul/li[{i}]/div[1]/div[6]/div[2]/span/text()")
    imglink = img[0] if img else '暂无数据'
    title = title_text[0] if title_text else '暂无数据'
    titlelink = title_href[0] if title_href else '暂无数据'
    dl = diZhi_left[0] if diZhi_left else '暂无数据'
    dr = diZhi_rigth[0] if diZhi_rigth else '暂无数据'
    address = dl.strip() + '--' + dr.strip()
    housedescribe = house_type[0] if house_type else '暂无数据'
    label = ''
    if len(kuaiSu) > 0:
        for a in kuaiSu:
            label += a.strip() + '  '
    else:
        label = '暂无数据'
    j = jiaGe[0] if jiaGe else '暂无数据'
    jd = jiaGe_danWei[0] if jiaGe_danWei else '暂无数据'
    price = j.strip() + jd.strip()
    squareprice = jiaGe_m[0] if jiaGe_m else '暂无数据'

    return imglink, title, titlelink, address, housedescribe, label, price, squareprice

